<template>
    <div class="good-list">
        <el-button type="text" icon="el-icon-arrow-left" @click="back">返回</el-button>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="商品名称" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="price">
                <el-input v-model.number="ruleForm.price"></el-input>
            </el-form-item>
            <el-form-item label="商品类别" prop="type">
                <el-checkbox-group v-model="ruleForm.type">
                    <el-checkbox v-for="item in goodtype" :label="item._id" name="type">{{item.typename}}</el-checkbox>
                </el-checkbox-group>
            </el-form-item>
            <el-form-item label="是否热门">
                <el-switch v-model="ruleForm.delivery"></el-switch>
            </el-form-item>
            <el-form-item>
                <el-upload
                    action="http://localhost:8888/api/upload"
                    list-type="picture-card"
                    :on-success="handleAvatarSuccess"
                    :on-preview="handlePictureCardPreview"
                    :on-remove="handleRemove">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible" size="tiny">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item> 
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立刻添加</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import api from '../../axios.js'

export default {
    data(){
        var checkprice = (rule, value, callback) => {
        if (!value) {
            return callback(new Error('价格不能为空'));
            }
            setTimeout(() => {
                if (!Number.isInteger(value)) {
                    callback(new Error('请输入数字值'));
                } else {
                    callback();
                }
            }, 1000);
        };
        return {
            msg:'hhhh',
            dialogImageUrl: '',
            //imgurl:[],
            dialogVisible: false,
            ruleForm: {
                name: '',
                delivery: false,
                type: [],
                price:'',
                imgurl:[],
            },
            rules: {
                name: [
                    { required: true, message: '请输入商品名称', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                type: [
                    { type: 'array', required: true, message: '请至少选择一个类别', trigger: 'change' }
                ],
                price: [
                    { required: true, validator: checkprice, trigger: 'blur' }
                ]
            },
            goodtype:[],
            goodid:0
        };
    },
    created(){
        api.getgoodtype().then((data) => {
            let result = data.data
            if(result.status == 'success'){
                this.goodtype = result.result
            }
        })
    },
    mounted(){
        console.log(this.$route.params.id)
        this.goodid = this.$route.params.id
        if(this.goodid == 0){
            return
        }else{
            let obj = {
                _id:this.goodid 
            }
            api.getgoodinfo(obj).then((data) => {
                let res = data.data
                if(res.status == 'success'){
                    if(res.data){
                        this.ruleForm = {
                            name: res.data.goodname,
                            delivery: res.data.ishot,
                            type: res.data.goodtype,
                            price:res.data.goodprice,
                            imgurl:res.data.img
                        }
                        //this.dialogVisible = true
                        this.dialogImageUrl = res.data.img[0]
                    }
                    
                }
            })
        }
    },
    methods: {
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        handlePictureCardPreview(file) {
            console.log(file)
            //this.dialogImageUrl = file.url;
            this.dialogVisible = true;
        },
        handleAvatarSuccess(res, file) {
            //let img = URL.createObjectURL(file.raw)
            this.dialogImageUrl = res.data.pictureUrl;
            let img = res.data.pictureUrl
            this.ruleForm.imgurl.push(img)
        },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let par = this.ruleForm
            api.addgood(par).then((data) => {
                let result = data.data
                if(result.status == 'success'){
                    this.$message({
                        message:result.message,
                        type:'success'
                    })
                    // this.ruleForm = {
                    //     name: '',
                    //     delivery: false,
                    //     type: [],
                    //     price:'',
                    //     imgurl:[]
                    // }
                    // this.dialogImageUrl = ''
                    this.resetForm('ruleForm')
                    this.$router.go(-1)
                }else{
                    this.$message({
                        message:result.message,
                        type:'error'
                    })
                }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      back(){
          this.$router.go(-1)
      }
    }
}
</script>

<style style="stylus">
    
</style>